package com.kmzx.one.project.controller;

import com.kmzx.one.common.utils.MyAssert;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.others.dto.UserAddressInfo;
import com.kmzx.one.project.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "快美甄选_[ 用户_地址_接口 ]")
@RestController
public class UserAddressController extends BaseController {

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 我的收货地址
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "1.我的收货地址", notes = "我的收货地址",position = -1)
    @GetMapping(value = "userAddress/mine", produces = "application/json;charset=UTF-8")
    public BaseResult listMyAddress(@RequestParam("userId") String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");

            List<UserAddressInfo> info = userAddressService.findAllAddress(userId);
            List<UserAddressInfo> priorities = info.stream().filter(f -> f.getIsPriority() == 1).collect(Collectors.toList());
            //无优先地址，默认取最新添加的地址
            if (priorities.size() == 0 && info.size() > 0) {
                List<UserAddressInfo> sorts = info.stream().sorted(Comparator.comparing(UserAddressInfo::getCreateTime).reversed()).collect(Collectors.toList());
                sorts.get(0).setIsPriority(1);
                return BaseResult.Builder.SUCC().initSuccData(sorts);
            }
            return BaseResult.Builder.SUCC().initSuccData(info);
        } catch (Exception e) {
            return handleException(e);
        }
    }

    /**
     * 新增收货地址
     *
     * @param address
     * @return
     */
    @ApiOperation(value = "2.新增收货地址", notes = "新增收货地址",position = -1)
    @PostMapping(value = "userAddress/add", produces = "application/json;charset=UTF-8")
    public BaseResult userAddressAdd(UserAddressInfo address) {
        try {
            beforeAopRepeat(">>>".concat(address.getUserId()).concat(address.getCity()));
            Integer allAddress = 0;
            String addressId = address.getAddressId();
            address.setIsPriority(0);//是否优先 默认否
            if (addressId != null && !"".equals(addressId)) {
                allAddress = userAddressService.userAddressEdit(address);
            } else {
                allAddress = userAddressService.userAddressAdd(address);
            }
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(allAddress);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    /**
     * 删除收货地址
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "3.删除收货地址", notes = "删除收货地址",position = -1)
    @GetMapping(value = "userAddress/del", produces = "application/json;charset=UTF-8")
    public BaseResult delUserAddress(@RequestParam("userId") String userId, String addressId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(addressId, "addressId 为null");
            int allAddress = userAddressService.delUserAddress(userId, addressId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(allAddress);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    /**
     * 设置默认地址
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "4.设置默认地址", notes = "设置默认地址",position = -1)
    @GetMapping(value = "userAddress/default", produces = "application/json;charset=UTF-8")
    public BaseResult setUserAddressDefault(String userId, String addressId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(addressId, "addressId 为null");
            int allAddress = userAddressService.setUserAddressDefault(userId, addressId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(allAddress);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    /**
     * 检测是否发货
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "5.检测是否发货", notes = "检测是否发货",position = -1)
    @GetMapping(value = "userAddress/delivery", produces = "application/json;charset=UTF-8")
    public BaseResult checkAddressDelivery(String userId, String addressId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(addressId, "addressId 为null");
            int allAddress = userAddressService.checkAddressDelivery(userId, addressId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(allAddress);
            success.setMsg("受道路限行影响,您所选择的地址暂不发货,给您带来的不便敬请谅解.");
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


}
